package com.aquarium.ui.controller;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.config.ModuleName;
import com.aquarium.module.ModuleController;
import com.aquarium.ui.ConnectDialogActivity;
import com.aquarium.ui.controller.ModuleSwitchableLayout.DoNowButtonListenerBinder;

public class ModuleControllerActivity extends Activity {

	public final static int REQUEST_CONNECT_WATER_MOTOR = 1;
	public final static int REQUEST_CONNECT_RPOTEIN_SKIMMER = 2;
	public final static int REQUEST_CONNECT_WAVE_MOTOR_1 = 3;
	public final static int REQUEST_CONNECT_WAVE_MOTOR_2 = 4;

	public final static int CONNECT_SECONDS = 5;

	private ModuleController mModuleController;
	private ArrayList<String> mConnectedModules;

	private LinearLayout mWaterMotorLayout;
	private LinearLayout mProteinSkimmerLayout;
	private LinearLayout mUVLightLayout;
	private LinearLayout mDosingPumpLayout;
	private LinearLayout mWaveMotorLayout1;
	private LinearLayout mWaveMotorLayout2;

	private ViewGroup mViewGroup;

	private ImageView mBackView;

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ModuleController.MESSAGE_COMMAND_REPLY:
				Intent intent = new Intent(
						ConnectDialogActivity.ACTION_CONNECT_SUCESS);
				sendBroadcast(intent);
				break;

			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.module_ctrl);

		mModuleController = ModuleController.getInstance(this);
		mConnectedModules = mModuleController.getConnectedModules();
		initLayout();
	}

	protected void setDoNowBtnBinder(ModuleSwitchableLayout layout) {
		layout.setDoNowButtonListenerBinder(new DoNowButtonListenerBinder() {

			@Override
			public boolean binder(String moduleName, RadioButton openRadioBtn,
					RadioButton closeRadioBtn) {
				if (ModuleName.WAVE_MOTOR_1.equals(moduleName)) {
					int waveMode = ((WaveMotorLayout) mWaveMotorLayout1)
							.getCurrentWaveModeInt();
					if (openRadioBtn.isChecked()) {
						mModuleController.sendWaveMotorCommand(
								ModuleName.WAVE_MOTOR_1, 1, waveMode, true,
								mHandler);
					} else if (closeRadioBtn.isChecked()) {
						mModuleController.sendWaveMotorCommand(
								ModuleName.WAVE_MOTOR_1, 1, waveMode, true,
								mHandler);
					}
				} else if (ModuleName.WAVE_MOTOR_2.equals(moduleName)) {
					int waveMode = ((WaveMotorLayout) mWaveMotorLayout2)
							.getCurrentWaveModeInt();
					if (openRadioBtn.isChecked()) {
						mModuleController.sendWaveMotorCommand(
								ModuleName.WAVE_MOTOR_2, 2, waveMode, true,
								mHandler);
					} else if (closeRadioBtn.isChecked()) {
						mModuleController.sendWaveMotorCommand(
								ModuleName.WAVE_MOTOR_2, 2, waveMode, true,
								mHandler);
					}
				} else {
					return false;
				}

				return true;
			}
		});
	}

	private void initLayout() {
		findViews();
		setButtonListener();
		String moduleMode = "";
		boolean isModuleOn = false;

		for (String module : mConnectedModules) {
			if (module.equals(ModuleName.WATER_MOTOR)) {
				mWaterMotorLayout = new ModuleSwitchableLayout(this, mHandler,
						ModuleName.WATER_MOTOR, WaterMotorSetting.class,
						REQUEST_CONNECT_WATER_MOTOR);
				mViewGroup.addView(mWaterMotorLayout);
				moduleMode = mModuleController.getWaterMotorMode();
				isModuleOn = mModuleController.isWaterMotorOn();
				setModuleModeView((ModuleSwitchableLayout) mWaterMotorLayout,
						moduleMode);
				((ModuleSwitchableLayout) mWaterMotorLayout)
						.setRadioButtonOpen(isModuleOn);

			} else if (module.equals(ModuleName.PROTEIN_SIMMMER)) {
				mProteinSkimmerLayout = new ModuleSwitchableLayout(this,
						mHandler, ModuleName.PROTEIN_SIMMMER,
						ProteinSkimmerSetting.class,
						REQUEST_CONNECT_RPOTEIN_SKIMMER);
				mViewGroup.addView(mProteinSkimmerLayout);
				moduleMode = mModuleController.getProteinSkimmerMode();
				isModuleOn = mModuleController.isProteinSkimmerOn();
				setModuleModeView(
						(ModuleSwitchableLayout) mProteinSkimmerLayout,
						moduleMode);
				((ModuleSwitchableLayout) mProteinSkimmerLayout)
						.setRadioButtonOpen(isModuleOn);

			} else if (module.equals(ModuleName.UV_LIGHT)) {
				mUVLightLayout = new ModuleUnswitchableLayout(this, mHandler,
						ModuleName.UV_LIGHT, UVLightSetting.class);
				mViewGroup.addView(mUVLightLayout);

			} else if (module.equals(ModuleName.DOSING_PUMP)) {
				mDosingPumpLayout = new ModuleUnswitchableLayout(this,
						mHandler, ModuleName.DOSING_PUMP,
						DosingPumpSetting.class);
				mViewGroup.addView(mDosingPumpLayout);
			} else if (module.equals(ModuleName.WAVE_MOTOR_1)) {
				mWaveMotorLayout1 = new WaveMotorLayout(this, mHandler,
						ModuleName.WAVE_MOTOR_1, WaveMotorSetting.class,
						REQUEST_CONNECT_WAVE_MOTOR_1);
				mViewGroup.addView(mWaveMotorLayout1);
				setDoNowBtnBinder((ModuleSwitchableLayout) mWaveMotorLayout1);
				moduleMode = mModuleController.getWaveMotorMode(1);
				isModuleOn = mModuleController.isWaveMotorOn(1);
				setModuleModeView((ModuleSwitchableLayout) mWaveMotorLayout1,
						moduleMode);
				((ModuleSwitchableLayout) mWaveMotorLayout1)
						.setRadioButtonOpen(isModuleOn);
			} else if (module.equals(ModuleName.WAVE_MOTOR_2)) {
				mWaveMotorLayout2 = new WaveMotorLayout(this, mHandler,
						ModuleName.WAVE_MOTOR_2, WaveMotorSetting.class,
						REQUEST_CONNECT_WAVE_MOTOR_2);
				mViewGroup.addView(mWaveMotorLayout2);
				setDoNowBtnBinder((ModuleSwitchableLayout) mWaveMotorLayout2);
				moduleMode = mModuleController.getWaveMotorMode(2);
				isModuleOn = mModuleController.isWaveMotorOn(2);
				setModuleModeView((ModuleSwitchableLayout) mWaveMotorLayout2,
						moduleMode);
				((ModuleSwitchableLayout) mWaveMotorLayout2)
						.setRadioButtonOpen(isModuleOn);
			}

		}
	}

	private void setModuleModeView(ModuleSwitchableLayout layout, String mode) {
		if (ModuleController.PREFER_VALUE_MODULE_AUTO.equals(mode)) {
			layout.setSwitcher(true);
		} else if (ModuleController.PREFER_VALUE_MODULE_MANU.equals(mode)) {
			layout.setSwitcher(false);
		}
	}

	private void findViews() {
		mViewGroup = (ViewGroup) findViewById(R.id.parent);
		mBackView = (ImageView) findViewById(R.id.back);
	}

	private void setButtonListener() {
		mBackView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_OK:
			switch (requestCode) {
			case REQUEST_CONNECT_WATER_MOTOR:
				boolean isWMChecked = ((ModuleSwitchableLayout) mWaterMotorLayout)
						.isOpenRadioButtonChecked();
				mModuleController.setWaterMotorOnOff(isWMChecked);
				break;
			case REQUEST_CONNECT_RPOTEIN_SKIMMER:
				boolean isPSChecked = ((ModuleSwitchableLayout) mProteinSkimmerLayout)
						.isOpenRadioButtonChecked();
				mModuleController.setProteinSkimmerOnOff(isPSChecked);
				break;
			case REQUEST_CONNECT_WAVE_MOTOR_1:
				boolean isWave1Checked = ((ModuleSwitchableLayout) mWaveMotorLayout1)
						.isOpenRadioButtonChecked();
				mModuleController.setWaveMotorOnOff(1, isWave1Checked);
				break;
			case REQUEST_CONNECT_WAVE_MOTOR_2:
				boolean isWave2Checked = ((ModuleSwitchableLayout) mWaveMotorLayout2)
						.isOpenRadioButtonChecked();
				mModuleController.setWaveMotorOnOff(2, isWave2Checked);
				break;
			default:
				break;
			}
			break;
		case RESULT_CANCELED:
			switch (requestCode) {
			case REQUEST_CONNECT_WATER_MOTOR:
				((ModuleSwitchableLayout) mWaterMotorLayout)
						.switchRadioButton();
				break;
			case REQUEST_CONNECT_RPOTEIN_SKIMMER:
				((ModuleSwitchableLayout) mProteinSkimmerLayout)
						.switchRadioButton();
				break;
			case REQUEST_CONNECT_WAVE_MOTOR_1:
				((ModuleSwitchableLayout) mWaveMotorLayout1)
						.switchRadioButton();
				break;
			case REQUEST_CONNECT_WAVE_MOTOR_2:
				((ModuleSwitchableLayout) mWaveMotorLayout2)
						.switchRadioButton();
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}
}
